﻿Public Class frmThongKe

    Private Sub btnThoat_Click(sender As System.Object, e As System.EventArgs) Handles btnThoat.Click

    End Sub

    Private Sub btnTim_Click(sender As System.Object, e As System.EventArgs) Handles btnTim.Click
        Dim tuNgay As New DateTime(txtTuNgay.Value.Year, txtTuNgay.Value.Month, txtTuNgay.Value.Day, 0, 0, 0)
        Dim denNgay As New DateTime(txtDenNgay.Value.Year, txtDenNgay.Value.Month, txtDenNgay.Value.Day, 23, 59, 59)

        Using db = New QlqaContext()
            lvHoaDon.Items.Clear()
            For Each dh In db.DonHangs.Where(Function(x) x.ThoiGian < denNgay And x.ThoiGian > tuNgay And x.DaThanhToan).ToList().Select(Function(x) New DonHang() With {.Id = x.Id, .Ban = db.Bans.ToList().FirstOrDefault(Function(s) s.Id = x.Ban.Id), _
                                                            .DaThanhToan = x.DaThanhToan, _
                                                            .ThanhTien = x.ThanhTien, .ThoiGian = x.ThoiGian, _
                                                            .ChiTietDonHangs = db.ChiTietDonHangs.Where(Function(c) c.DonHang.Id = x.Id).ToList().Where(Function(o) o.DonHang.Id = x.Id).ToList().Select(Function(c) New ChiTietDonHang() With {.Id = c.Id, .DonGia = c.DonGia, .DonHang = c.DonHang, .SanPham = db.SanPhams.ToList().FirstOrDefault(Function(s) s.Id = c.SanPham.Id), .SoLuong = c.SoLuong}).ToList()}).ToList()
                Dim i = New ListViewItem()
                i.Tag = dh
                i.SubItems.Add(dh.Ban.Ten)
                i.SubItems.Add(dh.ThanhTien.Format())
                i.SubItems.Add(dh.ThoiGian.ToString("dd/MM/yyyy hh:mm"))
                lvHoaDon.Items.Add(i)


            Next
            CapNhatSoThuTuHoaDon()
        End Using

    End Sub

    Private Sub CapNhatSoThuTuHoaDon()
        For i As Integer = 1 To lvHoaDon.Items.Count
            lvHoaDon.Items(i - 1).Text = i.ToString()
        Next
    End Sub
    Private Sub CapNhatSoThuTuMatHang()
        For i As Integer = 1 To lvMatHang.Items.Count
            lvMatHang.Items(i - 1).Text = i.ToString()
        Next
    End Sub
    Private Sub lvHoaDon_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvHoaDon.SelectedIndexChanged
        If lvHoaDon.SelectedItems.Count = 0 Then Exit Sub
        Dim ctx As DonHang = lvHoaDon.SelectedItems(0).Tag

        If ctx IsNot Nothing Then
            lvMatHang.Items.Clear()
            For Each o In ctx.ChiTietDonHangs
                Dim i = New ListViewItem()
                i.Tag = o
                i.SubItems.Add(o.SanPham.Ten)
                i.SubItems.Add(o.SoLuong)
                i.SubItems.Add(o.DonGia)
                i.SubItems.Add((o.DonGia * o.SoLuong).Format())
                lvMatHang.Items.Add(i)
            Next
        End If
        lblBan.Text = lvHoaDon.SelectedItems(0).SubItems(1).Text
        lblThoiGianThanhToan.Text = String.Format("Lúc: {0}", lvHoaDon.SelectedItems(0).SubItems(3).Text)
        txtThanhTien.Value = ctx.ThanhTien
        CapNhatSoThuTuMatHang()
    End Sub
End Class